package cn.kimming.common.util;

import sun.util.calendar.LocalGregorianCalendar;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: 刘铭轩KimmingLau
 * @Date: 2020-12-11 0:15
 */
public class DateUtil {

    private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
    private static SimpleDateFormat timestampFormat = new SimpleDateFormat("yyyyMMddHHmmss");

    private DateUtil() {}

    public static String dateTime2String(Date dateTime) {
        return dateTimeFormat.format(dateTime);
    }

    public static Date String2DateTime(String dateTime) {
        try {
            return dateTimeFormat.parse(dateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String date2String(Date date) {
        return dateFormat.format(date);
    }

    public static String time2String(Date date) {
        return timeFormat.format(date);
    }


    /*
        根据时间戳生成员工编号
        员工编号: 时间戳 + 2位随机数
     */
    public static String genEmpNumber() {
        return genBusinessId(2);
    }

    /*
        根据时间戳生成订单编号
        员工编号: 时间戳 + 4位随机数
     */
    public static String genOrderId() {
        return genBusinessId(4);
    }

    /*
        根据时间戳生成业务号
        格式: 年月日时分秒 + n位随机数
     */
    public static String genBusinessId(int n) {
        String format = timestampFormat.format(new Date());
        StringBuilder sb = new StringBuilder(format.length() + n + 1);
        sb.append(format);
        for (int i = 0; i < n; i++) {
            sb.append((int)(Math.random() * 10));
        }
        return sb.toString();
    }

    /**
     * 获取最近20天的日期字符串; 格式: yyyy-MM-dd
     * 如今日为2000-01-01, 则返回2000-01-01至2000-01-20的字符串数组
     * @return
     */
    public static List<Date> getRecent20Dates() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);

        List<Date> dates = new ArrayList<>(20);
        dates.add(c.getTime());

        for (int i = 1; i < 20; i++) {
            c.add(Calendar.DAY_OF_MONTH, 1);
            dates.add(c.getTime());
        }
        return dates;
    }

    /**
     * 获取今天之后的第n天
     * @return
     */
    public static Date getAfterDate(int n) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        c.add(Calendar.DAY_OF_MONTH, n);
        return c.getTime();
    }

    /**
     * 获取[begin, end]范围内的每一天
     * 注意: begin, end以及返回的所有日期中只包括日期
     * @param begin
     * @param end
     * @return
     */
    public static List<Date> getBetweenDates(Date begin, Date end) {
        List<Date> dates = new ArrayList<>();
        Calendar c = Calendar.getInstance();
        c.setTime(begin);
        while (c.getTime().compareTo(end) <= 0) {
            dates.add(c.getTime());
            c.add(Calendar.DAY_OF_MONTH, 1);
        }
        return dates;
    }




    /**
     * 判断日期是否为周末
     */
    public static boolean isWeekend(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dow = calendar.get(Calendar.DAY_OF_WEEK);
        return dow == 7 || dow == 1;
    }


    public static void main(String[] args) throws ParseException {
        System.out.println(genBusinessId(4));
    }


}
